package listnode;

import listnode.data.ListNode;


/**
 * 合并两个有序链表
 *
 * 遍历迭代
 */
public class MergeTwoLists {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode preHead=new ListNode(-1);
        ListNode pre=preHead;

        while (list1!=null&&list2!=null){
            if (list1.val<=list2.val){
                pre.next=list1;
                list1=list1.next;
            }else{
                pre.next=list2;
                list2=list2.next;
            }
            pre=pre.next;
        }

        pre.next=list1==null?list2:list1;

        return preHead.next;
    }

    public static void main(String[] args) {
        System.out.println("Hello and welcome!");
    }
}
